var _extend = function(model){
	model.snapshot;
	model.takeSnapshot = function(){
		model.snapshot = JSON.stringify(model.getRequestData());
	};
	model.loading = ko.observable(false);
	// Вспомагательные функции
	// Проверяет, существует ли объект в базе
	model.isNewRecord = function(){
		return model.id() === null;
	};
	model.isSame = function(compareModel){
		return compareModel !== null && compareModel.id() === model.id();
	};
	// Действия
	model.create = function(callbacks){
		callbacks = callbacks || {};
		callbacks.success = callbacks.success || function(){};
		callbacks.error = callbacks.error || function(){};
		callbacks.complete = callbacks.complete || function(){};
		model.loading(true);
		$.ajax({
			url: model.urls().create,
			data: model.getRequestData(),
			type: 'POST',
			dataType:'json',
			success: function(response){
				if(response.status == 'success'){
					callbacks.success(response);
					model.id(response.model.id);
				}
				else{
					alert(response.error);
				}
			},
			error: function(response){
				callbacks.error(response);
			},
			complete: function(){
				callbacks.complete();
				model.loading(false);
			}
		});
	};
	model.update =  function(callbacks){
		if(model.snapshot != JSON.stringify(model.getRequestData())){
			callbacks = callbacks || {};
			callbacks.success = callbacks.success || function(){};
			callbacks.error = callbacks.error || function(){};
			callbacks.complete = callbacks.complete || function(){};
			model.loading(true);
			$.ajax({
				url: model.urls().update,
				data: model.getRequestData(),
				type: 'POST',
				dataType:'json',
				success: function(response){
					if(response.status == 'success'){
						callbacks.success(response);
						model.takeSnapshot();
					}
					else{
						alert(response.error);
					}
				},
				error: function(response){
					callbacks.error(response);
				},
				complete: function(){
					callbacks.complete();
					model.loading(false);
				}
			});
		}
	};
	model.delete = function(callbacks){
		callbacks = callbacks || {};
		callbacks.success = callbacks.success || function(){};
		callbacks.error = callbacks.error || function(){};
		callbacks.complete = callbacks.complete || function(){};
		model.loading(true);
		$.ajax({
			url: model.urls().delete,
			data: model.getRequestData(),
			type: 'POST',
			dataType:'json',
			success: function(response){
				if(response.status == 'success'){
					callbacks.success(response);
				}
				else{
					alert(response.error);
				}
			},
			error: function(response){
				callbacks.error(response);
			},
			complete: function(){
				callbacks.complete();
				model.loading(false);
			}
		});
	};
};